package com.ruoyi.common.utils.file;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.io.IOUtils;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.aspectj.util.FileUtil;

import java.io.*;

public class ALIOSSUtils {


//    public static void main(String[] args) throws Exception {
//        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
//        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
//        String accessKeyId = "LTAI4Fkv8qrcoE9269MnLNuY";
//        String accessKeySecret = "xC5KIGNqY53yawYKFmCLJfCIwGUO8S";
//        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        // 填写Bucket名称，例如examplebucket。Bucket名称在OSS范围内必须全局唯一。
//        String bucketName = "yqlcloud";
//        String objectName = "test/exampleobject.java";
//        // https://yqlcloud.oss-cn-beijing.aliyuncs.com/test/exampleobject.java
////        byte[] data = new byte[10];
//        byte[] bytes = FileUtil.readAsByteArray(new File("E:\\java_workspack\\material\\ruoyi-common\\src\\main\\java\\com\\ruoyi\\common\\utils\\file\\FileTypeUtils.java"));
//        System.out.println(uploadOneFile(accessKeyId, accessKeySecret, endpoint, bucketName, objectName, bytes));
//    }

    /**
     * 阿里云文件上传
     *
     * @param accessKeyId     appId
     * @param accessKeySecret appSecret
     * @param endpoint        oss域名
     * @param bucketName      oss名称
     * @param objectName      文件名称
     * @param data            文件内容
     * @return 文件上传完成以后的完成路径
     */
    public static String uploadOneFile(String accessKeyId, String accessKeySecret, String endpoint, String bucketName, String objectName, byte[] data) {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 创建存储空间。
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(data));
            return endpoint.replace("oss", bucketName + "." + "oss") + "/" + objectName;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
            return null;
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
